<script setup lang="ts">
import TableTemplate from './components/TableTemplate.vue';
import DialogTemplate from './components/dialogTemplate.vue';
import {ElMessage} from "element-plus";
import {
  editCertification, editCertificationRule,
  exportCertificationList, getCertificationList, getCertificationRule, smartAudit,
} from "../account/accountApi.js";
const loading = ref(false)
const options = ref([
      { label: '准确', value: 0 },
      { label: '模糊', value: 1 },
      { label: '不做校验', value: 2 }
    ])
let dataSet = reactive({
  queryBox: [
    {
      model: 'username',
      type: 'input',
      placeholder: '请输入用户名称'
    },{
      model: 'workNumber',
      type: 'input',
      placeholder: '请输入工号'
    },{
      model: 'mobile',
      type: 'input',
      placeholder: '请输入手机号'
    }, {
      model: 'email',
      type: 'input',
      placeholder: '请输入邮箱'
    },
    {
      model: 'userType',
      type:'select',
      placeholder: '认证类型',
      options: [
        { label: '学生', value: 0 },
        { label: '教师', value: 1 },
        { label: '管理员', value: 2 },
      ],
    }
  ],
  tableList: [
    {prop: 'username', label: '用户名', width: '200%'},
    {prop: 'workNumber', label: '工号', width: '200'},
    {prop: 'mobile', label: '手机号', width: '200'},
    {prop: 'email', label: '邮箱', width: '250'},
    {prop: 'createDate', label: '创建时间', width: '250'},
  ],
  tableListSubmit: [
    {prop: 'status', label: '认证状态', width: '100', stutasColor: 'danger'},
  ],
  tnQueryTop: [
    {name: '导出',click: 'userExport', type: 'primary'},
    {name: '智能审核设置',click: 'userSmartSetting', type: 'success'}
  ],
  operationBtns: [
    {name: '审核', click: 'userEdit', type: 'primary'},
    {name: '智能审核', click: 'smartSetting', type: 'danger'},
    {name: '详情', click: 'userDetail', type: 'info'},
  ],
  tableData:{
    total: 0,
    rows: []
  }
})
// 提交状态
const getSubmitColor = (data) => {
  switch (data) {
    case 0:
      return 'primary';
    case 1:
      return 'info';
    case 2:
      return 'danger';
    default:
      return 'success';
  }
}
// 提交状态文字
const getStatusText = (data) => {
  switch (data.status) {
    case 0:
      return '待认证';
    case 1:
      return '通过';
    case 2:
      return '不通过';
    case 3:
      return '智能审核通过';
    default:
      return '驳回';
  }
}

let batchAddVisible = reactive({
  fileName: '用户批量添加Excel模板.xlsx',
  dialogFormVisibles: false,
})
// 弹窗 数据
const registerDialogRef = ref(null)
const registerTableRef = ref(null)
const title = ref('')
const dialogTemplateRef = ref(null)
const dialogTemplateVisible = ref(false)
const formItems = ref([{
  list: [
    {
      label: '用户名',
      prop: 'username',
      type: 'input',
      disabled: true,
      placeholder: '请输入用户名'
    },
    {
      label: '认证号',
      prop: 'workNumber',
      type: 'input',
      disabled: true,
      placeholder: '请输入工号'
    },
    {
      label: '手机号',
      prop: 'mobile',
      type: 'input',
      disabled: true,
      placeholder: '请输入手机号'
    },
    {
      label: '认证类型',
      prop:'userType',
      type:'select',
      disabled: true,
      options: [
        { label: '学生', value: 0 },
        { label: '教师', value: 1 },
        { label: '管理员', value: 2 },
      ],
      placeholder: '请选择性别'
    },
    {
      label: '认证资料',
      prop: 'fileList',
      type: 'uploadImage',
      multiple: true,
      limit: 1,
      disabled: true,
      placeholder: '请上传项目图片'
    },
    {
      label: '邮箱',
      prop: 'email',
      type: 'input',
      disabled: true,
      placeholder: '请输入邮箱'
    },
    {
      label: '认证状态',
      prop: 'status',
      type: 'select',
      options: [
        {label: "通过", value: 1}
        ,{label: "不通过", value: 2}
        ,{label: "驳回", value: 4}
      ],
      placeholder: '请选择状态'
    },
    {
      label: '备注',
      prop: 'content',
      type: 'input',
      placeholder: '请输入备注'
    },
  ]
}])
// 表单数据
const initialData = ref({})
//查看为true，其他为false
const isViewMode = ref(false)
// 操作名称 新增，编辑 详情
const operationName = ref('')
const handleDialogSubmit = async (data) => {
  if (operationName.value === '编辑'){
    await editCertification(data).then(res => {
      if (res.code === 0){
        ElMessage.success('审核成功')
        dialogTemplateVisible.value = false;
        dialogTemplateRef.value.dialogTemplateVisible = false;
        selectSubmit({ pageNum: 1, pageSize: 10 });
        return;
      }
      ElMessage.error(res.message || '编辑失败')
    });
  }
  else {
    await editCertificationRule(data).then(res => {
      if (res.code === 0){
        ElMessage.success('设置成功')
        dialogTemplateVisible.value = false;
        dialogTemplateRef.value.dialogTemplateVisible = false;
        return;
      }
      ElMessage.error(res.message || '设置失败')
    });
  }
}
// 重置
const resetSubmit = (data) => {
  selectSubmit(data);
}
// 查询
const selectSubmit = async (data) => {
  loading.value = true
  await getCertificationList(data).then(res => {
    dataSet.tableData.rows = res.data.records
    dataSet.tableData.total = res.data.total
  }).finally(() => {
        loading.value = false;
  });
}
selectSubmit({ pageNum: 1, pageSize: 10})
const handleIndex = ref([])
const handleChange = (data) => {
  handleIndex.value = data
}
// 导出
const userExport = () => {
  if (handleIndex.value.length < 1) {
    ElMessage.error("请选择要导出的数据！")
    return
  }
  exportCertificationList(handleIndex.value);
  handleIndex.value = [];
  registerTableRef.value?.clearSelection();
}
// 审核
const userEdit = (data) => {
  if (data.status !== 0 && data.status!== 3){
    ElMessage.warning("该条数据已审核！")
    return
  }
  operationName.value = '编辑'
  isViewMode.value = false
  title.value = '用户认证'
  initialData.value = { ...data }
  dialogTemplateVisible.value = true;
  dialogTemplateRef.value.dialogTemplateVisible = true;
}

// 智能审核   人工审核通过和智能审核通过都可以 跳过
const smartSetting =(data) => {
  if (data.status=== 1 || data.status=== 3){
    ElMessage.warning("该条数据已审核！")
    return;
  }
  ElMessageBox.confirm('是否确认智能审核？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
   .then(async()  =>  {
    await smartAudit({...data}).then(res => {
        if (res.code === 0){
          ElMessage.success('审核成功')
          return;
        }
        ElMessage.error(res.message || '设置失败')
      });
   })
   .catch((reason) => {
     ElMessage.info('已取消审核!')
   })
}


// 详情
const userDetail = (data) => {
  operationName.value = '详情'
  isViewMode.value = true
  title.value = '用户认证详情'
  initialData.value = { ...data }
  dialogTemplateVisible.value = true;
  dialogTemplateRef.value.dialogTemplateVisible = true;
}
const userSmartSetting = async () => {
  operationName.value='智能审核'
  isViewMode.value = false
  title.value = '智能审核设置'
  formItems.value = [{
    list: [
      {
        label: '审核类型',
        prop: 'userType',
        type:'select',
        isSelected: true,
        options: [
          { label: '学生', value: 0 },
          { label: '教师', value: 1 },
          { label: '管理员', value: 2 },
        ],
        placeholder: '请选择认证用户类型'
      },
      {
        label: '姓名',
        prop: 'username',
        type: 'select',
        options: options.value,
        placeholder: '包含姓名时'
      },
      {
        label: '认证号',
        prop: 'workNumber',
        type: 'select',
        options: options.value,
        placeholder: '包含工号时'
      },
      {
        label: '手机号',
        prop: 'mobile',
        type: 'select',
        options: options.value,
        placeholder: '包含手机号时'
      },{
        label: '邮箱',
        prop: 'email',
        type: 'select',
        options: options.value,
        placeholder: '包含邮箱时'
      },
      {
        label: '认证资料',
        prop: 'fileList',
        type: 'select',
        options: [
          { label: '跳过', value: 0 },
          { label: '人工审核', value: 1 },
        ],
        placeholder: '包含认证资料时'
      },
      {
        label: '备注',
        prop: 'content',
        type: 'input',
        placeholder: '请输入备注'
      },
    ]
  }]
  await getCertificationRule(0).then(res => {
    if (res.code === 0){
      initialData.value=res.data;
    }
  })
  dialogTemplateVisible.value = true;
  dialogTemplateRef.value.dialogTemplateVisible = true;
}
const dialogSelect = async (data) => {
  await getCertificationRule(data).then(res => {
    if (res.code === 0){
      initialData.value=res.data;
    }
  })
}

</script>

<template>
  <div>
    <TableTemplate
      ref="registerTableRef"
      :data-set="dataSet"
      @userEdit="userEdit"
      @smartSetting="smartSetting"
      :getStatusColor="getStatusColor"
      @userDetail="userDetail"
      :getSubmitColor="getSubmitColor"
      :get-status-text="getStatusText"
      @reset-submit="resetSubmit"
      @userExport="userExport"
      @handleChange="handleChange"
      @select-submit="selectSubmit"
      @userSmartSetting="userSmartSetting"
      :loading="loading"
    />
    <DialogTemplate
      v-model="dialogTemplateVisible"
      ref="dialogTemplateRef"
      :title="title"
      @dialog-select="dialogSelect"
      :width="'500px'"
      :initial-data="initialData"
      :form-items="formItems"
      :is-view="isViewMode"
      @handleDialogSubmit="handleDialogSubmit"
      :operation-name="operationName"
    />
  </div>

</template>

<style scoped lang="scss">

</style>
